GB_ChartDataManager.dataDomain="../";
var QS_CurrenciesChart=function(container,callback){
	this.container=container;
	this.staticValue={
		minWidth:150,
		winTitle:26,
		minHeight:100,
		barWidth:14
	};
	this.profile={
		subChartConfig:{
			showTab:true,
			YlabelWidth:50,
			showLabel:false,
			autToDraw:true
		},
		currencies:{
			"gbp":{
				txt:"GBP",
				color:"#CE0000",
				chartTxt:"1 GBP",
				tickers:{
					"euro":{"symbol":"245:GBPEURFLIT","PID":"CU$$GBPEUR"},//PID need to check color
					"us":  { "symbol":"245:GBPUSDFLIT","PID":"CU$$GBPUSD"},
					"yen": {"symbol":"245:GBPJPYFLIT","PID":"CU$$GBPJPY"}
				}
			},
			"euro":{
				txt:"Euro",
				color:"#0000FF",
				chartTxt:"1 Euro",
				tickers:{
					"us":{"symbol":"245:EURUSDFLIT","PID":"CU$$EURUSD"},
					"yen":{"symbol":"245:EURJPYFLIT","PID":"CU$$EURJPY"},
					"gbp":{"symbol":"245:EURGBPFLIT","PID":"CU$$EURGBP"}
				}
			},
			"us":{
				txt:"US-Dollar",
				color:"#63A521",
				chartTxt:"1 US Dollar",
				tickers:{
					"euro":{"symbol":"245:USDEURFLIT","PID":"CU$$USDEUR"},
					"yen":{"symbol":"245:USDJPYFLIT","PID":"CU$$USDJPY"},
					"gbp":{"symbol":"245:USDGBPFLIT","PID":"CU$$USDGBP"}
				}
			},
			"yen":{
				txt:"Yen",
				color:"#FFAD00",
				chartTxt:"100 Yen",
				tickers:{
					"euro":{"symbol":"245:JPYEURFLIT","PID":"CU$$JPYEUR"},
					"us":{"symbol":"245:JPYUSDFLIT","PID":"CU$$JPYUSD"},
					"gbp":{"symbol":"245:JPYGBPFLIT","PID":"CU$$JPYGBP"}
				}
			}
		},
		showComments:true,
		pading:[0,0,5,10],
		isAPI:true,
		fixHeight:true,
		tempW:120,
		tempH:720,
		className:""
	};
	this.callback=callback;
	this.currenciesChart={};
	this.loaded=false;
	this.commentHeight=0;
	this.currenciesChartTickers={};
};
QS_CurrenciesChart.prototype={
	init:function(id,profile){
		$.extend(true,this.profile,profile);
		this.id=id||new Date().getTime();
		this.currencyTotal=this._countCurrencies();
		this._genUI();
		this._genCurrencyChart();
		if(!this.profile.isAPI){
			if(this.profile.height){
	            this.profile.height-=this.staticValue.winTitle;
	        }else{
	        	this.profile.tempH-=this.staticValue.winTitle;
	        }
        }
		var css={};
		css["width"]=  Math.max(this.staticValue.minWidth,this.profile.width||this.profile.tempW);
        css["height"]=  Math.max(this.staticValue.minHeight,this.profile.height||this.profile.tempH);
		this.el.css(css);
	},
	_genUI:function(){
		var html='<div class="rtq-currencieschart'+this.profile.className+'">'+
		'<div class="rtq-chartarea  clearfix"></div><div class="rtq-commentarea"></div>'+
		'</div>';
		this.el = $(html).appendTo(this.container);
		this.chartArea=this.el.find(".rtq-chartarea");
		this.commentsArea=this.el.find(".rtq-commentarea");
		if(!this.profile.showComments){
			this.commentsArea.hide();
		}else{
			this.commentsArea.append(this._genComments());
			var commentsCss={};
			if(this.profile.pading.length>0){
				commentsCss["margin"]=String.format("0px {0}px 0px {1}px",this.profile.pading[1],this.profile.pading[3]);
			}
			this.commentsArea.css(commentsCss);
		}
	},
	_genCurrencyChart:function(){
		var currencyObj=null,cfg={
			margin:this.profile.pading
		},callback={},currencyCfg={};
		if(this.profile.width){
			cfg["width"]=Math.floor((this.profile.width)/this.currencyTotal);
		}else{
			cfg["tempW"]=Math.floor((this.profile.tempW)/this.currencyTotal);
		}
		if(this.profile.height){
			cfg["height"]=this.profile.height - (this.profile.showComments?this.commentsArea.outerHeight():0);
		}else{
			cfg["tempH"]=(this.profile.tempH) - (this.profile.showComments?this.commentsArea.outerHeight():0);
		}
		for(var currency in this.profile.currencies){
			currencyCfg={};
			currencyObj=this.profile.currencies[currency];
			currencyCfg.tickers= this.currenciesChartTickers[currency];//[]
			currencyCfg.chartText= currencyObj.chartTxt;//chart text
			if(typeof currencyObj.YlabelWidth!='undefined'){
				currencyCfg.YlabelWidth = currencyObj.YlabelWidth;
			}
			this.currenciesChart[currency]=new QS_CurrencyChart(this.chartArea,callback);
			if(this.profile.menuHeight){
				this.currenciesChart[currency].setTabMenu(this.profile.menuHeight);
			}
			this.currenciesChart[currency].init(String.format("{0}_{1}",this.id,currency),$.extend(true,{},this.profile.subChartConfig,cfg,currencyCfg));
		}
	},
	_countCurrencies:function(){
		var count=0,tempColor={};
		for(var currency in this.profile.currencies){
			count++;
			tempColor[currency]=this.profile.currencies[currency].color;
		}
		this._genCurrencyChartProfile(tempColor);
		return count;
	},
	_genCurrencyChartProfile:function(tempColor){
		var currencyTicker=null,tempCurrency=this.profile.currencies;
		for(var currency in tempCurrency){
			currencyTicker = tempCurrency[currency]["tickers"];
			this.currenciesChartTickers[currency]=[];
			for(var otherCurrency in currencyTicker){
				this.currenciesChartTickers[currency].push({
					ticker:currencyTicker[otherCurrency]["symbol"],
					color:tempColor[otherCurrency],
					PID:currencyTicker[otherCurrency]["PID"]||currencyTicker[otherCurrency]["symbol"]
				});
			}
		}
	},
	_genComments:function(){
		var str='',currencyObj=null;
		for(var currency in this.profile.currencies){
			currencyObj=this.profile.currencies[currency];
			str+=String.format('<span class="rtq-currencycomments"><span class="rtq-currencycolor" style="background-color:{0}"></span><span class="rtq-currencytext">{1}</span></span>',currencyObj["color"],currencyObj["txt"]);
		}
		this.commentsArea.append(str);
		this.commentHeight=this.commentsArea.outerHeight();
	},
	onHeightChange:function(height){
		if (!this.loaded) {
            this.lastCallbackHeight = height;
            if (typeof this.callback.onLoad == 'function') {
                this.loaded = true;
                this.callback.onLoad({
                    component: 'minChart',
                    height: height,
                    minWidth:this.staticValue.minWidth*this.currencyTotal
                });
            }
        }else {
            if (this.lastCallbackHeight != height) {
                this.lastCallbackHeight = height;
                if (typeof this.callback.onHeightChange == 'function') {
                    this.callback.onHeightChange({
                        height: this.lastCallbackHeight,
                        component: 'minChart'
                    });
                }
            }
        }
	},
	changeOutSize:function(w,h){
		//alert("change size"+w+":"+h);
        if(this.profile.isAPI){
    		w = this.profile.width||w||this.container.width();
    		h = this.profile.height||h||this.container.height();
        }else{
            //if it is terminal, the height of win-bd is static
            w = w||this.container.parent().width();
            h = h||this.container.parent().height();
            this.el.css({
                width: "auto"
            });
        }
		w=Math.max(this.staticValue.minWidth,w);
		h=Math.max(this.staticValue.minHeight,h);
		this._changeOutSizeChart(w,h);
		this.el.css({width: w,height:h});
		this.onHeightChange(h);
	},
	_changeOutSizeChart:function(w,h){
		var width=Math.floor(w/this.currencyTotal),height=h - this.commentHeight;
		for(var currency in this.currenciesChart){
			this.currenciesChart[currency].changeOutSize(width,height);
		}
	},
	clear:function(){
		for(var chart in this.currenciesChart){
			this.currenciesChart[chart].clear();
		}
		for (var p in this) {
            if (!p || p == 'clear') {
                continue;
            }
            this[p] = null;
            delete this[p];
        }
	}
};
